package net.office.controller;

import net.office.bean.BaseDict;
import net.office.bean.Memorandum;
import net.office.bean.StaffInfo;
import net.office.service.BaseDictService;
import net.office.service.MemorandumService;
import net.office.utils.TemplateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class MemorandumController {
    @Autowired
    private MemorandumService memorandumService;

    @Autowired
    private BaseDictService baseDictService;

    /*查询列表*/
    @RequestMapping("/memo/list.do")
    public String list(HttpSession session, Model model){

        StaffInfo staffInfo = (StaffInfo) session.getAttribute("staff");

        List<Memorandum> memoList = memorandumService.findAllByStaffId(staffInfo.getStaffId());

        for(int i=0; i<memoList.size(); i++){
            if(memoList.get(i).getWorkStatus() != null) {
                memoList.get(i).setBaseDict(baseDictService.findOneByDictId(memoList.get(i).getWorkStatus()));
            }
        }

        model.addAttribute("memoList", memoList);

        List<BaseDict> baseDictList = baseDictService.findByDictTypeCode(TemplateParam.WORK_STATUS_CODE);
        model.addAttribute("baseDictList", baseDictList);

        return "memo";
    }

    /*搜索列表*/
    @RequestMapping("/memo/search.do")
    public String search(@RequestParam(value = "assignProjectName", required = false) String assignProjectName,
                         @RequestParam(value = "assignStaffName", required = false) String assignStaffName,
                         @RequestParam(value = "workStartDate", required = false) Date workStartDate,
                         @RequestParam(value = "workPlanDate", required = false) Date workPlanDate,
                         @RequestParam(value = "workFinishDate", required = false) Date workFinishDate,
                         HttpSession session, Model model){

        Memorandum memorandum = new Memorandum();

        StaffInfo staffInfo = (StaffInfo) session.getAttribute("staff");
        memorandum.setStaffId(staffInfo.getStaffId());

        if(assignProjectName!=null && assignProjectName.trim().length()!=0){
            memorandum.setAssignProjectName(assignProjectName);
        }
        if(assignStaffName!=null && assignStaffName.trim().length()!=0){
            memorandum.setAssignStaffName(assignStaffName);
        }
        if(workStartDate != null){
            memorandum.setWorkStartDate(workStartDate);
        }
        if(workPlanDate != null){
            memorandum.setWorkPlanDate(workPlanDate);
        }
        if(workFinishDate != null){
            memorandum.setWorkFinishDate(workFinishDate);
        }

        List<Memorandum> memoList = memorandumService.findByMemorandum(memorandum);

        for(int i=0; i<memoList.size(); i++){
            if(memoList.get(i).getWorkStatus() != null) {
                memoList.get(i).setBaseDict(baseDictService.findOneByDictId(memoList.get(i).getWorkStatus()));
            }
        }

        model.addAttribute("memoList", memoList);

        List<BaseDict> baseDictList = baseDictService.findByDictTypeCode(TemplateParam.WORK_STATUS_CODE);
        model.addAttribute("baseDictList", baseDictList);

        return "memo";
    }

    /*新增*/
    @RequestMapping("/memo/insert.do")
    public String insert(@RequestParam(value = "assignProjectName", required = false) String assignProjectName,
                         @RequestParam(value = "assignStaffName", required = false) String assignStaffName,
                         @RequestParam(value = "workStartDate", required = false) Date workStartDate,
                         @RequestParam(value = "workPlanDate", required = false) Date workPlanDate,
                         @RequestParam(value = "workFinishDate", required = false) Date workFinishDate,
                         @RequestParam(value = "workContent", required = false) String workContent,
                         @RequestParam(value = "workOverdueReason", required = false) String workOverdueReason,
                         @RequestParam(value = "workDealResult", required = false) String workDealResult,
                         @RequestParam(value = "workStatus", required = false) Integer workStatus,
                         HttpSession session){

        Memorandum memorandum = new Memorandum();
        StaffInfo staffInfo = (StaffInfo) session.getAttribute("staff");
        memorandum.setStaffId(staffInfo.getStaffId());

        if(assignProjectName!=null && assignProjectName.trim().length()!=0){
            memorandum.setAssignProjectName(assignProjectName);
        }
        if(assignStaffName!=null && assignStaffName.trim().length()!=0){
            memorandum.setAssignStaffName(assignStaffName);
        }
        if(workStartDate != null){
            memorandum.setWorkStartDate(workStartDate);
        }
        if(workPlanDate != null){
            memorandum.setWorkPlanDate(workPlanDate);
        }
        if(workFinishDate != null){
            memorandum.setWorkFinishDate(workFinishDate);
        }
        if(workContent!=null && workContent.trim().length()!=0){
            memorandum.setWorkContent(workContent);
        }
        if(workOverdueReason!=null && workOverdueReason.trim().length()!=0){
            memorandum.setWorkOverdueReason(workOverdueReason);
        }
        if(workDealResult!=null && workDealResult.trim().length()!=0){
            memorandum.setWorkDealResult(workDealResult);
        }
        if(workStatus!=null){
            memorandum.setWorkStatus(workStatus);
        }

        int res = memorandumService.insert(memorandum);

        if(res > 0){
            return "redirect:/memo/list.do";
        }

        return "memo-add";
    }

    /*修改*/
    @RequestMapping("/memo/update.do")
    public String update(@RequestParam(value = "memorId") Integer memorId,
                         @RequestParam(value = "assignProjectName", required = false) String assignProjectName,
                         @RequestParam(value = "assignStaffName", required = false) String assignStaffName,
                         @RequestParam(value = "workStartDate", required = false) Date workStartDate,
                         @RequestParam(value = "workPlanDate", required = false) Date workPlanDate,
                         @RequestParam(value = "workFinishDate", required = false) Date workFinishDate,
                         @RequestParam(value = "workContent", required = false) String workContent,
                         @RequestParam(value = "workOverdueReason", required = false) String workOverdueReason,
                         @RequestParam(value = "workDealResult", required = false) String workDealResult,
                         @RequestParam(value = "workStatus", required = false) Integer workStatus,
                         HttpSession session){

        Memorandum memorandum = new Memorandum();
        StaffInfo staffInfo = (StaffInfo) session.getAttribute("staff");
        memorandum.setStaffId(staffInfo.getStaffId());

        memorandum.setMemorId(memorId);

        if(assignProjectName!=null && assignProjectName.trim().length()!=0){
            memorandum.setAssignProjectName(assignProjectName);
        }
        if(assignStaffName!=null && assignStaffName.trim().length()!=0){
            memorandum.setAssignStaffName(assignStaffName);
        }
        if(workStartDate != null){
            memorandum.setWorkStartDate(workStartDate);
        }
        if(workPlanDate != null){
            memorandum.setWorkPlanDate(workPlanDate);
        }
        if(workFinishDate != null){
            memorandum.setWorkFinishDate(workFinishDate);
        }
        if(workContent!=null && workContent.trim().length()!=0){
            memorandum.setWorkContent(workContent);
        }
        if(workOverdueReason!=null && workOverdueReason.trim().length()!=0){
            memorandum.setWorkOverdueReason(workOverdueReason);
        }
        if(workDealResult!=null && workDealResult.trim().length()!=0){
            memorandum.setWorkDealResult(workDealResult);
        }
        if(workStatus!=null){
            memorandum.setWorkStatus(workStatus);
        }

        int res = memorandumService.updateByPrimaryKey(memorandum);

        if(res > 0){

        }

        return "redirect:/memo/toEdit.do?memorId="+memorId+"";
    }


    /*删除单条(ajax)*/
    @RequestMapping("/memo/deleteOne.do")
    @ResponseBody
    public Object deleteOne(@RequestParam("memorId") Integer memorId){

        int res = memorandumService.deleteByPrimaryKey(memorId);

        Map<String, String> map = new HashMap<String, String>();
        if(res > 0){
            map.put("res", "1");
        }else{
            map.put("res", "0");
        }
        return map;
    }

    /*跳转到新增页面*/
    @RequestMapping("/memo/toAdd.do")
    public String toAdd(Model model){
        List<BaseDict> baseDictList = baseDictService.findByDictTypeCode(TemplateParam.WORK_STATUS_CODE);
        model.addAttribute("baseDictList", baseDictList);
        return "memo-add";
    }
    /*跳转到修改页面*/
    @RequestMapping("/memo/toEdit.do")
    public String toEdit(@RequestParam("memorId") Integer memorId, Model model){
        Memorandum memorandum = memorandumService.findOneByMemorId(memorId);
        model.addAttribute("memorandum", memorandum);

        List<BaseDict> baseDictList = baseDictService.findByDictTypeCode(TemplateParam.WORK_STATUS_CODE);
        model.addAttribute("baseDictList", baseDictList);

        return "memo-edit";
    }
}
